Free memory outside function [migrated]

Posted by Dev Bag on Programmers See other posts from Programmers or by Dev Bag
Published on 2012-04-10T23:54:53Z Indexed on 2012/04/11 5:43 UTC
Read the original article Hit count: 158

Filed under:
|

Can you please help with this issue, is the below gonna leak memory or is it ok? and please let me know if there is something else that I need to pay attention to

typedef struct 
{
int len;
UC * message; 
}pack;

pack * prepare_packet_to_send(const int length,const unsigned char tag,const int numargs, ... )
{

pack *layer=  malloc(sizeof(pack));
va_list listp;
va_start( listp, numargs );
int step = 0;
layer->message = (unsigned char *) malloc(length);
layer->len = length;
int i = 0;
int len = 0;
unsigned char *source_message ; 

for( i = 0 ; i < numargs; i++ )
{

source_message = va_arg( listp, unsigned char *);
len = va_arg( listp, long);
memcpy(layer->message+step, source_message, (long) len);
step+=len;
}

va_end( listp );
 return layer;
}


main()

{

pack *test = call prepare_packet_to_send(sizeof(var1)+sizeof(var2),any tag,any args) 

// are following two frees correct/enough? or is there something else i need to do to     prevent mem leak?

free(test->message);
free(test);


}

© Programmers or respective owner

Related posts about c

    Related posts about memory-leak